<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>09canvas折线图-绘制网格</title>
</head>
<style>
    canvas {
        border: 1px solid #ccc;
    }
</style>
<body>
<canvas width="600" height="400"></canvas>
<script>
    var myCanvas = document.querySelector('canvas');
    var ctx = myCanvas.getContext('2d');
    /*1.绘制网格*/
    /*2.网格的大小*/
    var gridSize = 20;
    /*3.以便历的方式确定x轴y轴的线的条数 x轴与画布长度，y轴与画布高度*/
    /*    var canvasWidth=myCanvas.width;
    var canvasHeight=myCanvas.height;
    console.log(canvasWidth,canvasHeight);*/
    var canvasWidth = ctx.canvas.width;
    var canvasHeight = ctx.canvas.height;
    var xLineTotal = Math.floor(canvasHeight / gridSize);
    var yLineTotal = Math.floor(canvasWidth / gridSize);
    for (var i = 0; i < xLineTotal; i++) {
        ctx.beginPath();
        ctx.moveTo(0, i * gridSize - 0.5);
        ctx.lineTo(canvasWidth, i * gridSize - 0.5);
        ctx.strokeStyle = '#ddd';
        ctx.stroke();
    }
    for (var i = 0; i < yLineTotal; i++) {
        ctx.beginPath();
        ctx.moveTo(i * gridSize - 0.5, 0);
        ctx.lineTo(i * gridSize - 0.5, canvasHeight);
        ctx.strokeStyle = '#ddd';
        ctx.stroke();
    }
    console.log(canvasWidth, canvasHeight);

</script>
</body>
</html>

